<?php

namespace AdminModule;

class AuthorizationPresenter extends \BasePresenter
{

	public function renderDefault()
	{
		$authorizationModel = $this->context->models->authorization;
		$this->template->roles = $authorizationModel->getRoles();
	}

	public function renderAddRole()
	{
		
	}

	public function renderEditRole($id)
	{
		$authorizationModel = $this->context->models->authorization;
		$role = $authorizationModel->getRole(array('id' => $id));
		$this->template->role = $role['name'];
		$priorities = $this['editRoleForm']['priority']->getItems();
		unset($priorities[$role['priority']]);
		$this['editRoleForm']['priority']->setItems($priorities);
		$values = iterator_to_array($role);
		$values['priority']--;
		$this['editRoleForm']->setValues($values);
	}
	
	/** @secured */
	public function handleDeleteRole($id)
	{
		$authorizationModel = $this->context->models->authorization;
		if ($authorizationModel->deleteRole($id))
		{
			$this->flashMessage('Role byla úspěšně smazána');
		} else
		{
			$this->flashMessage('Chyba! Role nelze smazat, pokud existuje uživatel s danou rolí!', 'error');
		}
		$this->redirect('this');
	}

	public function createComponentPermissionForm($name)
	{
		$form = new \Nette\Application\UI\Form($this, $name);
		$authorizationModel = $this->context->models->authorization;
		$permissionsInRoles = $authorizationModel->getPermissionsInRolesArray();
		$permissions = $authorizationModel->getPermissions();
		foreach ($authorizationModel->getRoles() as $role)
		{
			$form->addGroup($role['name'])->setOption('description', \Nette\Utils\Html::el('div'));
			$container = $form->addContainer($role['role']);
			foreach ($permissions as $permission)
			{
				$in_container = (isset($container[$permission['resource']]) ? $container[$permission['resource']] : $container->addContainer($permission['resource']));
				$checkbox = $in_container->addCheckbox($permission['privilege'], $permission['name']);
				if (!$form->isSubmitted() && isset($permissionsInRoles[$role['id']]) && in_array($permission['id'], $permissionsInRoles[$role['id']]))
				{
					$checkbox->setValue(true);
				}
			}
			$container->addSubmit('send', 'Uložit vše');
		}
		$form->setCurrentGroup();
		$form->addSubmit('send', 'Uložit vše');
		$form->onSuccess[] = callback($this, 'permissionFormSubmited');
		return $form;
	}

	public function permissionFormSubmited(\Nette\Application\UI\Form $form)
	{
		$values = $form->getValues();

		$authorizationModel = $this->context->models->authorization;
		$permissions = array();
		foreach ($values as $role => $resources)
		{
			$roleID = null;
			foreach ($resources as $resource => $privileges)
			{
				foreach ($privileges as $privilege => $value)
				{
					if ($value === TRUE)
					{
						$roleID = ($roleID != null ? $roleID : dereferenced_array_value($authorizationModel->getRole(array('role' => $role)), 'id'));
						$permissionID = dereferenced_array_value($authorizationModel->getPermission(array('resource' => $resource, 'privilege' => $privilege)), 'id');
						$permissions[$roleID][] = $permissionID;
					}
				}
			}
		}
		if ($authorizationModel->savePermissions($permissions))
		{
			$this->flashMessage('Oprávnění uložena');
			$this->redirect('this');
		} else
		{
			$this->flashMessage('Oprávnění nebyla uložena!', 'error');
			$this->redirect('this');
		}
	}

	public function createComponentAddRoleForm($name)
	{
		$authorizationModel = $this->context->models->authorization;
		$priorities = array(0 => '# Začátek #');
		foreach ($authorizationModel->getRoles() as $role)
		{
			$priorities[$role['priority']] = $role['name'];
		}
		$form = new \Nette\Application\UI\Form($this, $name);
		$form->addText('name', 'Název:')->setRequired('Vyplňte prosím název role');
		$form->addSelect('priority', 'Zařadit za:', $priorities);
		$form->addSubmit('send', 'Uložit');
		$form->onSuccess[] = callback($this, 'addRole');
		return $form;
	}

	public function createComponentEditRoleForm($name)
	{
		$authorizationModel = $this->context->models->authorization;
		$priorities = array(0 => '# Začátek #');
		foreach ($authorizationModel->getRoles() as $role)
		{
			$priorities[$role['priority']] = $role['name'];
		}
		$form = new \Nette\Application\UI\Form($this, $name);
		$form->addText('name', 'Název:')->setRequired('Vyplňte prosím název role');
		$form->addSelect('priority', 'Zařadit za:', $priorities);
		$form->addHidden('id');
		$form->addSubmit('send', 'Uložit');
		$form->onSuccess[] = callback($this, 'editRole');
		return $form;
	}

	public function addRole($form)
	{
		$values = $form->getValues();
		$authorizationModel = $this->context->models->authorization;
		if ($authorizationModel->addRole($values))
		{
			$this->flashMessage('Role byla úspěšně přidána');
			$this->redirect('default');
		} else
		{
			$this->flashMessage('Chyba! Tato role již existuje', 'error');
		}
	}

	public function editRole($form)
	{
		$values = $form->getValues();
		$authorizationModel = $this->context->models->authorization;
		if ($authorizationModel->editRole($values))
		{
			$this->flashMessage('Role byla úspěšně upravena');
			$this->redirect('default');
		} else
		{
			$this->flashMessage('Chyba při úpravě role!', 'error');
		}
	}

}